package com.matt.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.matt.system.domain.TSysuser;
import com.matt.system.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

@Controller
public class UserAction extends ActionSupport {
	
	/**
	 * 用户查询grid
	 */
	private static final long serialVersionUID = 1L;

	private long total;
	
	private List<Map<String, Object>> rows;
	
	private int start;
	
	private int limit;
	
	private String userCode;
	
	private String companyName;
	
	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	@Autowired
	private UserService userService;

	public long getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List<Map<String, Object>> getRows() {
		return rows;
	}

	public void setRows(List<Map<String, Object>> rows) {
		this.rows = rows;
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String getUsers(){
		rows = new ArrayList<Map<String,Object>>();
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TSysuser.class);
		if(userCode!=null&&userCode.length()>0){
			detachedCriteria.add(Restrictions.eq("TSuCode", this.getUserCode()).ignoreCase());
		}
		if(companyName!=null&&companyName.length()>0){
			detachedCriteria.add(Restrictions.like("TSuName","%" + this.getCompanyName()+"%"));
		}
		
		total = userService.getCountByCriteria(detachedCriteria);
		List<TSysuser> users = this.userService.findPageByCriteria(start, limit, detachedCriteria);
		for(TSysuser t :users){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("TSuCode", t.getTSuCode());
			map.put("TSuName", t.getTSuName());
			map.put("TSuCompanycode", t.getTSuCompanycode());
			map.put("TSuPhone", t.getTSuPhone());
			map.put("TSuEmail", t.getTSuEmail());
			map.put("TSuStime", t.getTSuStime());
			rows.add(map);
		}
		return "users";
	}
	
}
